﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLDC.Common.Estate;
using System.Data;
using ZLDC.Topo.Model.Estate;
using ZLDC.Topo.BLL.Estate;

namespace ZLDC.Estate.PM
{
    public partial class EstateUpdate : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                // ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadIDParamType(status, false, "状态");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(source, false, "来源");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(pocStatus, false, "房产证状态");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(houseStyle, false, "样式");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(direct, false, "朝向");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(fitment, false, "装修");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(useStatus, false, "使用状态");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(propertyRight, false, "产权");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadMarkParamType(status, true, "状态");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadIDParamType(tradeType, true, "交易(盘)");
                ZLDC.Topo.BLL.Estate.SystemParamBLL.LoadParamType(mark2, false, "营业税");
                mark2.Items.Insert(0, "请选择");
                LoadItem();
                userid.Value = _Utility.GetInfoBySession().m_sUserID;//用户ID
                deptid.Value = _Utility.GetInfoBySession().m_sDeptID;//部门ID

                if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("修改全部盘源"))
                {
                    mid.Value = "1";
                }
                if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("修改本部盘源"))
                {
                    updateId.Value = "1";
                }

                int id = _Request.GetInt("id", 0);
                if (id > 0)
                {
                    HousingInfo info = HousingBLL.p_housing_detail(id);
                    ///1.如果为资料，并且子状态非：资料转当前待审，垃圾转当前,可以查看业主电话 并且盘源为有效状态
                    ///2.本人盘源,或者本部，并且角色是经理可以查看业主电话
                    ///3.管理
                    if ((info.Status.ToString()=="2" && info.Status1.ToString()=="0"  && info.IsValid.ToString()=="0")|| userid.Value == info.MarkUId.ToString() || (deptid.Value == info.MarkUDeptId.ToString() && updateId.Value == "1") || mid.Value == "1")
                    {
                        ownerTel.Value = info.OwnerTel;
                        linkmanTel.Value = info.LinkmanTel;
                    }
                    else
                    {
                        ownerTel.Value = Enscript(info.OwnerTel);
                        linkmanTel.Value = Enscript(info.LinkmanTel);
                    }
                }

                //记录角色
                RoleId.Value = "";
               
            }
        }
        /// <summary>
        /// 加载房屋设施
        /// </summary>
        public void LoadItem()
        {
            DataTable dt = null;
            int total = 0;
            string condition = " where type='房屋设施'";

            string cachekey = "housingappliances";///设置房屋设施缓存，时间1个小时
            if (_Cache.GetCache(cachekey) == null || _Cache.GetCache(cachekey).ToString() == "")
            {
                using (_Database db = new _Database("estate"))
                {
                    dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_list_table(1, 50, condition, ref total, db);
                }
                string arr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;房屋设施";
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    if (i % 8 == 0 && i > 0)
                    {
                        arr += "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    arr += "<input id='hAppliances' name='hAppliances' type='checkbox' />" + dt.Rows[i]["item"].ToString() + "";
                }

                string arrList = arr + "<input id='btnCheck' class='button_bak' type='button' value='反选' onclick='SelectAll()' />";
                divhAppliances.InnerHtml = arrList;

                if (dt.Rows.Count > 0)
                    _Cache.SetCache(cachekey, arrList, 3600 * 1);
            }
            else 
            {
                divhAppliances.InnerHtml = _Cache.GetCache(cachekey).ToString();
            }
          
            
        }

        public string Enscript(string strTel)
        {
            string str = "";
            for (var i = 0; i < strTel.Length; i++)
            {
                string sTel = strTel.Substring(i, 1);
                switch (sTel)
                {
                    case "0":
                        str += "J";
                        break;
                    case "1":
                        str += "A";
                        break;
                    case "2":
                        str += "B";
                        break;
                    case "3":
                        str += "C";
                        break;
                    case "4":
                        str += "D";
                        break;
                    case "5":
                        str += "E";
                        break;
                    case "6":
                        str += "F";
                        break;
                    case "7":
                        str += "G";
                        break;
                    case "8":
                        str += "H";
                        break;
                    case "9":
                        str += "I";
                        break;
                    default:
                        str += sTel;
                        break;
                }
            }
            return str;
        }

        public string  Descript(string strTel) {
             string str = "";
             for (var i = 0; i < strTel.Length; i++) {
                 string sTel = strTel.Substring(i, 1);
                 switch (sTel) {
                     case "J":
                         str += "0";
                         break;
                     case "A":
                         str += "1";
                         break;
                     case "B":
                         str += "2";
                         break;
                     case "C":
                         str += "3";
                         break;
                     case "D":
                         str += "4";
                         break;
                     case "E":
                         str += "5";
                         break;
                     case "F":
                         str += "6";
                         break;
                     case "G":
                         str += "7";
                         break;
                     case "H":
                         str += "8";
                         break;
                     case "I":
                         str += "9";
                         break;
                     default:
                         str += sTel;
                         break;
                 }
             }
             return str;
         }
    }
}
